package com.bqtv.sport.ui.im

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.commen.dialog.MessageDialog
import com.bqtv.sport.databinding.ActivityGroupHomeBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.hjq.base.BaseDialog
import com.hjq.widget.view.SwitchButton
import com.tencent.imsdk.v2.V2TIMGroupChangeInfo
import com.tencent.imsdk.v2.V2TIMMessage
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuicore.interfaces.TUICallback
import com.tencent.qcloud.tuikit.timcommon.bean.GroupProfileBean
import com.tencent.qcloud.tuikit.timcommon.util.TUIUtil
import com.tencent.qcloud.tuikit.tuichat.TUIChatService
import com.tencent.qcloud.tuikit.tuichat.interfaces.GroupProfileListener
import com.tencent.qcloud.tuikit.tuichat.presenter.GroupProfilePresenter


class GroupHomeActivity : AppActivity<ActivityGroupHomeBinding>() {
    private lateinit var profileBean: GroupProfileBean
    private lateinit var groupProfilePresenter: GroupProfilePresenter
    private var groupId: String? = null

    companion object {
        public var INTENT_Group_ID = "group_id"
        fun start(context: Context, userId: String) {
            val intent = Intent(context, GroupHomeActivity::class.java)
            intent.putExtra(INTENT_Group_ID, userId)
            context.startActivity(intent)
        }
    }

    override fun getViewBinding() = ActivityGroupHomeBinding.inflate(layoutInflater)

    override fun initView() {
        groupId = intent.getStringExtra(INTENT_Group_ID)
        groupProfilePresenter = GroupProfilePresenter()

    }

    override fun initData() {
        groupProfilePresenter.loadGroupProfile(groupId)
        groupProfilePresenter.setGroupProfileListener(object : GroupProfileListener {

            override fun onGroupProfileLoaded(groupProfileBean: GroupProfileBean?) {
                setGroupProfile(groupProfileBean!!)
            }

            override fun onConversationCheckResult(
                isPinned: Boolean,
                isFolded: Boolean
            ) {
                binding.sbToTop.setChecked(isPinned)
            }

            override fun onGroupProfileChanged(changeInfo: V2TIMGroupChangeInfo?) {
                onGroupProfileChanged(changeInfo)
            }

            override fun onGroupMemberCountChanged(count: Int) {

            }
        });
        initListener()
    }

    fun setGroupProfile(profileBean: GroupProfileBean) {
        this.profileBean = profileBean

        binding.groupName.setText(profileBean.getGroupName())
        binding.tvId.setText(profileBean.getGroupID())

        loadGroupFaceUrl()
        if (profileBean.getRecvOpt() == V2TIMMessage.V2TIM_RECEIVE_NOT_NOTIFY_MESSAGE) {
            binding.sbMsgRev.setChecked(true)
        } else {
            binding.sbMsgRev.setChecked(false)
        }

        if (profileBean.isOwner()) {
            binding.tvExitGroup.setText(com.tencent.qcloud.tuikit.tuichat.R.string.dissolve)
        } else {
            binding.tvExitGroup.setText(com.tencent.qcloud.tuikit.tuichat.R.string.exit_group)
        }
    }

    private fun loadGroupFaceUrl() {
        val faceUrl = profileBean.getGroupFaceUrl()
        val defaultGroupFaceResID =
            TUIUtil.getDefaultGroupIconResIDByGroupType(getContext(), profileBean.getGroupType())
        Glide.with(TUIChatService.getAppContext())
            .load(faceUrl)
            .placeholder(defaultGroupFaceResID)
            .apply(RequestOptions().centerCrop().error(defaultGroupFaceResID))
            .into(binding.groupIcon)
    }

    private fun initListener() {
        binding.llExitGroup.setOnClickListener(View.OnClickListener { v: View? ->
            if (profileBean.isOwner()
                && (profileBean.getGroupType() != TUIConstants.GroupType.TYPE_WORK && profileBean.getGroupType() != TUIConstants.GroupType.TYPE_PRIVATE)
            ) {
                var msgDialog= MessageDialog.Builder(this)
                    .setTitle(com.bqtv.sport.R.string.kind_reminder)
                    .setMessage(com.bqtv.sport.R.string.exit_group_clear)
                    .setListener(object : MessageDialog.OnListener {
                        override fun onConfirm(dialog: BaseDialog?) {
                            dismissGroup()
                        }

                    })
                msgDialog.show()
            } else {
                var msgDialog= MessageDialog.Builder(this)
                    .setTitle(com.bqtv.sport.R.string.kind_reminder)
                    .setMessage(com.bqtv.sport.R.string.dissolve_group_clear)
                    .setListener(object : MessageDialog.OnListener {
                        override fun onConfirm(dialog: BaseDialog?) {
                            quitGroup()
                        }

                    })
                msgDialog.show()
            }
        })
        binding.sbToTop.setOnCheckedChangeListener(object : SwitchButton.OnCheckedChangeListener {
            override fun onCheckedChanged(
                button: SwitchButton,
                isChecked: Boolean
            ) {
                pinConversation(isChecked)
            }
        })
        binding.sbMsgRev.setOnCheckedChangeListener(object : SwitchButton.OnCheckedChangeListener {
            override fun onCheckedChanged(
                button: SwitchButton,
                isChecked: Boolean
            ) {
                groupProfilePresenter.setMessageRecvOpt(
                    profileBean.getGroupID(),
                    !isChecked,
                    object : TUICallback() {
                        override fun onSuccess() {
                            if (isChecked) {

                            } else {
                                groupProfilePresenter.setGroupFold(
                                    profileBean.getGroupID(),
                                    false,
                                    object : TUICallback() {
                                        override fun onSuccess() {}

                                        override fun onError(errCode: Int, errMsg: String?) {

                                        }
                                    })
                            }
                        }

                        override fun onError(errorCode: Int, errorMessage: String?) {

                        }
                    })
            }

        })

    }

    private fun dismissGroup() {
        if (groupProfilePresenter == null || profileBean == null) {
            return
        }
        groupProfilePresenter.dismissGroup(profileBean.getGroupID(), object : TUICallback() {
            override fun onSuccess() {
                (getContext() as Activity).finish()
            }

            override fun onError(errorCode: Int, errorMessage: String?) {}
        })
    }

    private fun quitGroup() {
        if (groupProfilePresenter == null || profileBean == null) {
            return
        }
        groupProfilePresenter.quitGroup(profileBean.getGroupID(), object : TUICallback() {
            override fun onSuccess() {
                (getContext() as Activity).finish()
            }

            override fun onError(errorCode: Int, errorMessage: String?) {}
        })
    }


    private fun pinConversation(isPin: Boolean) {
        if (groupProfilePresenter == null || profileBean == null) {
            return
        }
        groupProfilePresenter.pinConversation(profileBean.getGroupID(), isPin)
    }

    override fun onClick(view: View) {
        super.onClick(view)
        when (view) {

        }
    }
}